<template>
  <div class="login-container">
    <!-- 动态粒子背景 -->
    <div class="particles">
      <div v-for="i in 60" :key="i" class="particle"></div>
    </div>

    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 头部 -->
      <div class="card-header">
        <div class="logo-box">
          <img src="@/assets/logo.svg" class="logo" alt="logo">
          <h1 class="system-name">JioJio</h1>
        </div>
        <p class="welcome-text">欢迎回来，请登录您的账号</p>
      </div>

      <!-- 表单区域 -->
      <el-form
          :model="loginForm"
          :rules="rules"
          @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
              class="custom-input"
              tabindex="1"
          >
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
              class="custom-input"
              tabindex="2"
          >
          </el-input>
        </el-form-item>

        <el-button
            :loading="loading"
            native-type="submit"
            class="login-btn"
        >
          {{ loading ? '登录中...' : '立即登录' }}
        </el-button>
      </el-form>

      <!-- 底部链接 -->
      <div class="card-footer">
        <a class="link" @click="showForget = true">忘记密码?</a>
        <span class="divider">|</span>
        <router-link to="/register" class="link">注册账号</router-link>
      </div>
    </div>

    <!-- 背景装饰元素 -->
    <div class="decor decor-left"></div>
    <div class="decor decor-right"></div>
  </div>
</template>

<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useAuthStore } from '@/store/auth'
import {ElMessage} from "element-plus";

const authStore = useAuthStore()

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = reactive({
  username: [
    { required: true, trigger: 'blur', message: '用户名不能为空' },
    { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, trigger: 'blur', message: '密码不能为空' },
    { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
  ]
})

const loading = ref(false)
const showForget = ref(false)

const handleLogin = async () => {
  try {
    loading.value = true
    await authStore.login(loginForm)
  } catch (error) {
    ElMessage.error({
      message: error.message,
      duration: 3000
    })
    // 调试信息
    console.debug('错误完整响应:', error.response)
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  overflow: hidden;
  position: relative;

  .particles {
    position: absolute;
    width: 100%;
    height: 100%;

    .particle {
      position: absolute;
      background: rgba(255,255,255,0.15);
      border-radius: 50%;
      animation: float 20s infinite linear;

      @for $i from 1 through 60 {
        &:nth-child(#{$i}) {
          $size: random(20) + 10px;
          width: $size;
          height: $size;
          left: random(100) * 1%;
          top: random(100) * 1%;
          animation-delay: random(20) * -1s;
        }
      }
    }
  }

  .login-card {
    width: 420px;
    padding: 40px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    z-index: 1;
    position: relative;
    backdrop-filter: blur(10px);

    .card-header {
      text-align: center;
      margin-bottom: 40px;

      .logo-box {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 20px;

        .logo {
          width: 48px;
          height: 48px;
          margin-right: 12px;
        }

        .system-name {
          font-size: 24px;
          color: #2d3a4b;
          margin: 0;
          font-weight: 600;
          letter-spacing: 1px;
        }
      }

      .welcome-text {
        color: #606266;
        font-size: 14px;
        margin: 0;
      }
    }

    .custom-input {
      margin-bottom: 24px;

      :deep(.el-input__wrapper) {
        background: rgba(245, 247, 250, 0.9);
        border-radius: 8px;
        box-shadow: 0 0 0 1px rgba(228, 231, 237, 0.5);
        transition: all 0.3s;

        &:hover {
          box-shadow: 0 0 0 1px #c0c4cc;
        }

        &.is-focus {
          box-shadow: 0 0 0 2px #409eff !important;
        }
      }

      .input-icon {
        font-size: 18px;
        color: #909399;
      }
    }

    .login-btn {
      width: 100%;
      height: 44px;
      font-size: 16px;
      letter-spacing: 2px;
      background: linear-gradient(45deg, #409eff, #337ecc);
      border: none;
      border-radius: 8px;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
      }
    }

    .card-footer {
      margin-top: 24px;
      text-align: center;
      font-size: 14px;

      .divider {
        color: #dcdfe6;
        margin: 0 12px;
      }

      .link {
        color: #606266;
        cursor: pointer;
        transition: color 0.3s;

        &:hover {
          color: #409eff;
        }
      }
    }
  }

  .decor {
    position: absolute;
    width: 200px;
    height: 200px;
    background: linear-gradient(45deg, rgba(255,255,255,0.1), transparent);
    border-radius: 50%;
    filter: blur(60px);
    z-index: 0;

    &-left {
      left: -100px;
      bottom: -100px;
    }

    &-right {
      right: -100px;
      top: -100px;
    }
  }
}

@keyframes float {
  0% { transform: translateY(0) translateX(0); }
  25% { transform: translateY(-100px) translateX(50px); }
  50% { transform: translateY(-50px) translateX(-50px); }
  75% { transform: translateY(50px) translateX(100px); }
  100% { transform: translateY(0) translateX(0); }
}
</style>